package com.ctshk.app.order.goods.controller;

import com.alibaba.fastjson.JSONObject;
import com.ctshk.common.constant.RedisConstants;
import com.ctshk.common.enums.SystemError;
import com.ctshk.common.exception.BusinessException;
import com.ctshk.common.utils.RSAUtil;
import com.ctshk.common.web.support.BaseController;
import com.ctshk.rpc.order.goods.dto.ShoppingShopStoreDTO;
import org.apache.commons.lang3.StringUtils;
import org.apache.logging.log4j.util.Strings;
import org.springframework.stereotype.Component;

import javax.servlet.http.HttpServletRequest;

/**
 * TODO
 *
 * @author 黄利斌
 * @date 2021/6/11 9:25 上午
 */
@Component
public class AppBaseController extends BaseController {

    public ShoppingShopStoreDTO getShoppingShopStoreUser(HttpServletRequest request) {
        String token = request.getHeader(RedisConstants.JWT_TOKEN_HEADER);
        if (StringUtils.isBlank(token)) {
            throw new BusinessException(SystemError.USER_1032);
        }
        ShoppingShopStoreDTO shoppingShopStoreDTO = new ShoppingShopStoreDTO();
        try {
            token = token.replace(RedisConstants.JWT_TOKEN_PREFIX, Strings.EMPTY);
            String tokenDecrypt = RSAUtil.decryptStr(token);
            shoppingShopStoreDTO = JSONObject.parseObject(tokenDecrypt, ShoppingShopStoreDTO.class);
        } catch (Exception e) {
            throw new BusinessException(SystemError.USER_1015);
        }

        return shoppingShopStoreDTO;
    }
}
